// +----------------------------------------------------------------------
// | JavaWeb_Vue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2019~2020 南京JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <1175401194@qq.com>
// +----------------------------------------------------------------------

package com.javaweb.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.CommonUtils;
import com.javaweb.system.common.BaseQuery;
import com.javaweb.system.common.BaseServiceImpl;
import com.javaweb.system.constant.FeedbackConstant;
import com.javaweb.system.entity.Feedback;
import com.javaweb.system.mapper.FeedbackMapper;
import com.javaweb.system.query.FeedbackQuery;
import com.javaweb.system.service.IFeedbackService;
import com.javaweb.system.utils.ShiroUtils;
import com.javaweb.system.vo.feedback.FeedbackInfoVo;
import com.javaweb.system.vo.feedback.FeedbackListVo;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

/**
  * <p>
  * 用户反馈表 服务类实现
  * </p>
  *
  * @author 鲲鹏
  * @since 2021-06-04
  */
@Service
public class FeedbackServiceImpl extends BaseServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        FeedbackQuery feedbackQuery = (FeedbackQuery) query;
        // 查询条件
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        // 标题
        if (!StringUtils.isEmpty(feedbackQuery.getTitle())) {
            queryWrapper.like("title", feedbackQuery.getTitle());
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("id");

        // 获取数据列表
        IPage<Feedback> page = new Page<>(feedbackQuery.getPage(), feedbackQuery.getLimit());
        IPage<Feedback> pageData = feedbackMapper.selectPage(page, queryWrapper);
        pageData.convert(x -> {
            FeedbackListVo feedbackListVo = Convert.convert(FeedbackListVo.class, x);
            // 截图地址
            if (!StringUtils.isEmpty(feedbackListVo.getImage())) {
                feedbackListVo.setImage(CommonUtils.getImageURL(feedbackListVo.getImage()));
            }
            return feedbackListVo;
        });
        return JsonResult.success(pageData);
    }

    /**
     * 获取详情Vo
     *
     * @param id 记录ID
     * @return
     */
    @Override
    public Object getInfo(Serializable id) {
        Feedback entity = (Feedback) super.getInfo(id);
        // 返回视图Vo
        FeedbackInfoVo feedbackInfoVo = new FeedbackInfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, feedbackInfoVo);
        // 截图
        if (!StringUtils.isEmpty(feedbackInfoVo.getImage())) {
            feedbackInfoVo.setImage(CommonUtils.getImageURL(feedbackInfoVo.getImage()));
        }
        return feedbackInfoVo;
    }

    /**
     * 添加、更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult edit(Feedback entity) {
        // 截图
        if (entity.getImage().contains(CommonConfig.imageURL)) {
            entity.setImage(entity.getImage().replaceAll(CommonConfig.imageURL, ""));
        }
        if (entity.getId() != null && entity.getId() > 0) {
            entity.setUpdateTime(DateUtils.now());
            entity.setUpdateUser(1);
        } else {
            entity.setCreateTime(DateUtils.now());
            entity.setCreateUser(1);
        }
        return super.edit(entity);
    }

    /**
     * 删除记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult delete(Feedback entity) {
        entity.setUpdateTime(DateUtils.now());
        entity.setUpdateUser(1);
        entity.setMark(0);
        return super.delete(entity);
    }

}